/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifal.bd;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

/**
 *
 * @author VagnerSilva
 */
public class CadFuncionarioBD {

    public void insereCadFuncionarioBD(CadFuncioanrio f) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.getConnection();
        String sql = "insert into funcionario" + "(nome, email, endereco, dataNascimento)" + "values (?,?,?,?)";
        PreparedStatement stmt = con.prepareStatement(sql);

        stmt.setString(1, f.getNome());
        stmt.setString(2, f.getEmail());
        stmt.setString(3, f.getEndereco());

        java.sql.Date dataParaGravar = new java.sql.Date(
                Calendar.getInstance().getTimeInMillis());
        stmt.setDate(4, dataParaGravar);
        stmt.execute();
        stmt.close();
        con.close();
    }

    public CadFuncionarioBD buscaFuncionarioPeloId(int codigo) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.getConnection();
        String sql = "select * from funcionario where codigo=?";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setInt(1, codigo);
        ResultSet rs = stmt.executeQuery();
        CadFuncionario f = new Cadfuncionario();
        while (rs.next()) {
            f.setNome(rs.getString("nome"));
            f.setEndereco(rs.getString("Endereco"));
            f.setEmail(rs.getString("Email"));

        }
        rs.close();
        stmt.close();
        con.close();
        return f;


    }

    public void altera(CadFuncionario funcionario) {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.getConnection();
        String sql = "update funcionario set nome=?, email=?, endereco=?"
                + " where id=?";
        try {
            PreparedStatement stmt = con.prepareStatement(sql);
            stmt.setString(1, funcionario.getNome());
            stmt.setString(2, funcionario.getEmail());
            stmt.setString(3, funcionario.getEndereco());
            stmt.setLong(4, funcionario.getId());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void removeContato(CadFuncionario f) {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.getConnection();
        try {

            PreparedStatement stmt = con.prepareStatement("delete "
                    + "from funcionario where id=?");
            stmt.setLong(1, f.getId());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
